<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="uname">
      <el-input v-model="dataForm.uname" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pwd">
      <el-input v-model="dataForm.pwd" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="真实姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="真实姓名"></el-input>
    </el-form-item>
    <el-form-item label="电子邮件" prop="email">
      <el-input v-model="dataForm.email" placeholder="电子邮件"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="tel">
      <el-input v-model="dataForm.tel" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="冻结状态" prop="state">
      <el-input v-model="dataForm.state" placeholder="冻结状态"></el-input>
    </el-form-item>
    <el-form-item label="角色" prop="isSystem">
      <el-input v-model="dataForm.isSystem" placeholder="角色"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          uname: '',
          pwd: '',
          name: '',
          email: '',
          tel: '',
          remark: '',
          state: '',
          isSystem: ''
        },
        dataRule: {
          uname: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          pwd: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '真实姓名不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '电子邮件不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '冻结状态不能为空', trigger: 'blur' }
          ],
          isSystem: [
            { required: true, message: '角色不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/shop/admin/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.uname = data.admin.uname
                this.dataForm.pwd = data.admin.pwd
                this.dataForm.name = data.admin.name
                this.dataForm.email = data.admin.email
                this.dataForm.tel = data.admin.tel
                this.dataForm.remark = data.admin.remark
                this.dataForm.state = data.admin.state
                this.dataForm.isSystem = data.admin.isSystem
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/shop/admin/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'uname': this.dataForm.uname,
                'pwd': this.dataForm.pwd,
                'name': this.dataForm.name,
                'email': this.dataForm.email,
                'tel': this.dataForm.tel,
                'remark': this.dataForm.remark,
                'state': this.dataForm.state,
                'isSystem': this.dataForm.isSystem
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
